home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / OFPong1.0d1 / OFPong.of < prev   
Encoding:
Text File  |  1998-06-21  |  10.2 KB  |  502 lines  |  [TEXT/R*ch]

  1. dl
  2.  
  3. decimal
  4.  
  5. 0 value gscreen
  6. " screen" open-dev to gscreen
  7.  
  8. 0 value gkbd
  9. " kbd" open-dev to gkbd
  10.  
  11. 0 value erasecol
  12. -1 value drawcol
  13.  
  14. 0 value key_left_up
  15. 0 value key_left_down
  16. 0 value key_right_up
  17. 0 value key_right_down
  18. 0 value key_esc
  19. 0 value key_off
  20.  
  21. get-msecs value grandseed
  22. 0 value glastupdate
  23. 0 value loopcount
  24. 0 value totalupdate
  25.  
  26. 0 value ballstop
  27.  
  28. 0 value ballx
  29. 0 value bally
  30.  
  31. 0 value balldx
  32. 0 value balldy
  33.  
  34. 0 value leftbaty
  35. 0 value rightbaty
  36. 0 value batdy
  37.  
  38. 0 value leftscore
  39. 0 value rightscore
  40.  
  41. 640 value screenx
  42. 480 value screeny
  43.  
  44. : screen-prop@ ( prop-name prop-len -- value )
  45.   gscreen ihandle>phandle get-package-property
  46.   0= if
  47.     decode-int -rot 2drop
  48.   else
  49.     256
  50.   then
  51. ;
  52.  
  53. " width" screen-prop@ to screenx
  54. " height" screen-prop@ to screeny
  55.  
  56. 20 value ballsize
  57. ballsize 2 / value scoresize
  58. ballsize 5 * value batsize
  59. 1000 value pscale
  60.  
  61. screenx ballsize - pscale * value ball_limit_x
  62. ballsize pscale * value ball_limit_lo_y
  63. screeny ballsize 2 * - pscale * value ball_limit_hi_y
  64.  
  65. ballsize pscale * value bat_limit_lo_y
  66. screeny ballsize batsize + - pscale * value bat_limit_hi_y
  67.  
  68. 0 value hit_limit_left_lo_x
  69. ballsize 2 * pscale * value hit_limit_left_hi_x
  70. screenx ballsize 3 * - pscale * value hit_limit_right_lo_x
  71. screenx ballsize - pscale * value hit_limit_right_hi_x
  72.  
  73. ballsize pscale * value reflect_left_x
  74. screenx ballsize 2 * - pscale * value reflect_right_x
  75.  
  76. : random ( -- n ) grandseed 16807 * 17 + abs to grandseed grandseed 1000 mod ;
  77. : unscale ( n -- n ) pscale 2 / + pscale / ;
  78. : calcbatx ( n -- x ) screenx ballsize 3 * - * ballsize + ;
  79. : paintrect ( c pixx pixy pixw pixh -- ) " fill-rectangle" gscreen $call-method ;
  80.  
  81. : rectcol ( startx starty x1 y1 x2 y2 c -- startx starty )
  82.   { startx starty x1 y1 x2 y2 c }
  83.   
  84.   c
  85.   x1 scoresize * startx +
  86.   y1 scoresize * starty +
  87.   x2 x1 - scoresize *
  88.   y2 y1 - scoresize *
  89.   paintrect
  90.   
  91.   startx starty
  92. ;
  93.  
  94. : blackrect ( startx starty x1 y1 x2 y2 -- ) drawcol rectcol ;
  95. : whiterect ( startx starty x1 y1 x2 y2 -- ) erasecol rectcol ;
  96.  
  97. : drawblank ( startx starty -- startx starty )
  98.   0 0 4 7 whiterect
  99. ;
  100.  
  101. : drawzero ( startx starty -- startx starty )
  102.   0 0 1 7 blackrect
  103.   1 0 3 1 blackrect
  104.   1 6 3 7 blackrect
  105.   3 0 4 7 blackrect
  106.   1 3 3 4 whiterect
  107. ;
  108.  
  109. : drawone ( startx starty -- startx starty )
  110.   3 0 4 7 blackrect
  111.   0 0 3 7 whiterect
  112. ;
  113.  
  114. : drawtwo ( startx starty -- )
  115.   0 0 4 1 blackrect
  116.   3 1 4 3 blackrect
  117.   0 3 4 4 blackrect
  118.   0 4 1 6 blackrect
  119.   0 6 4 7 blackrect
  120.   0 1 1 3 whiterect
  121.   3 4 4 6 whiterect
  122. ;
  123.  
  124. : drawthree ( startx starty -- startx starty )
  125.   0 0 4 1 blackrect
  126.   3 1 4 3 blackrect
  127.   0 3 4 4 blackrect
  128.   3 4 4 6 blackrect
  129.   0 6 4 7 blackrect
  130.   0 1 1 3 whiterect
  131.   0 4 1 6 whiterect
  132. ;
  133.  
  134. : drawfour ( startx starty -- startx starty )
  135.   0 0 1 3 blackrect
  136.   0 3 3 4 blackrect
  137.   3 0 4 7 blackrect
  138.   1 0 3 1 whiterect
  139.   0 4 3 7 whiterect
  140. ;
  141.  
  142. : drawfive ( startx starty -- startx starty )
  143.   0 0 4 1 blackrect
  144.   0 1 1 3 blackrect
  145.   0 3 4 4 blackrect
  146.   3 4 4 6 blackrect
  147.   0 6 4 7 blackrect
  148.   3 1 4 3 whiterect
  149.   0 4 1 6 whiterect
  150. ;
  151.  
  152. : drawsix ( startx starty -- startx starty )
  153.   0 0 1 7 blackrect
  154.   1 3 3 4 blackrect
  155.   1 6 3 7 blackrect
  156.   3 3 4 7 blackrect
  157.   1 0 4 3 whiterect
  158. ;
  159.  
  160. : drawseven ( startx starty -- startx starty )
  161.   0 0 3 1 blackrect
  162.   3 0 4 7 blackrect
  163.   0 1 3 7 whiterect
  164. ;
  165.  
  166. : draweight ( startx starty -- startx starty )
  167.   0 0 4 1 blackrect
  168.   0 1 1 3 blackrect
  169.   3 1 4 3 blackrect
  170.   0 3 4 4 blackrect
  171.   0 4 1 6 blackrect
  172.   3 4 4 6 blackrect
  173.   0 6 4 7 blackrect
  174. ;
  175.  
  176. : drawnine ( startx starty -- startx starty )
  177.   0 0 1 4 blackrect
  178.   1 0 3 1 blackrect
  179.   1 3 3 4 blackrect
  180.   3 0 4 7 blackrect
  181.   0 4 3 7 whiterect
  182. ;
  183.  
  184. : drawdigit ( startx starty n -- )
  185.   { n }
  186.   n 0 = if drawzero then
  187.   n 1 = if drawone then
  188.   n 2 = if drawtwo then
  189.   n 3 = if drawthree then
  190.   n 4 = if drawfour then
  191.   n 5 = if drawfive then
  192.   n 6 = if drawsix then
  193.   n 7 = if drawseven then
  194.   n 8 = if draweight then
  195.   n 9 = if drawnine then
  196.   2drop
  197. ;
  198.  
  199. : drawnumber ( startx starty num -- )
  200.   { startx starty num }
  201.   startx starty num abs 100 mod 10 / drawdigit
  202.   startx scoresize 5 * + starty num abs 10 mod drawdigit
  203. ;
  204.  
  205. : plotball ( x y -- ) { x y } drawcol x unscale y unscale ballsize ballsize paintrect ;
  206. : eraseball ( x y -- ) { x y } erasecol x unscale y unscale ballsize ballsize paintrect ;
  207. : plotbat ( n y -- ) { n y } drawcol n calcbatx y unscale ballsize batsize paintrect ;
  208. : erasebat ( n y -- ) { n y } erasecol n calcbatx y unscale ballsize batsize paintrect ;
  209.  
  210. : redraw ( -- )
  211.   drawcol 0 0 screenx ballsize paintrect
  212.   drawcol 0 screeny ballsize - screenx ballsize paintrect
  213.  
  214.   drawcol screenx scoresize - 2 / ballsize 2 * scoresize screeny ballsize 4 * - paintrect
  215.   
  216.   ballsize 7 * ballsize 2 * leftscore drawnumber
  217.   screenx ballsize 7 * 9 scoresize * + - ballsize 2 * rightscore drawnumber
  218.   0 leftbaty plotbat
  219.   1 rightbaty plotbat
  220.   ballx bally plotball
  221. ;
  222.  
  223. : drawboard ( -- )
  224.   drawcol 0 0 screenx screeny paintrect
  225.   erasecol 0 0 screenx screeny paintrect
  226.   redraw
  227. ;
  228.  
  229. : resetball ( -- )
  230.   500 to ballstop
  231.   screenx ballsize - 2 / pscale * ballx pscale mod + random + to ballx
  232.   screeny ballsize - 2 / pscale * bally pscale mod + random + to bally
  233.   
  234.   random screenx pscale * * 2000000 / to balldx
  235.   random screeny pscale * * 2000000 / to balldy
  236.   balldx screenx pscale * 3000 / + to balldx
  237.   balldy screeny pscale * 6000 / + to balldy
  238.   
  239.   random 500 < if
  240.     balldx negate to balldx
  241.   then
  242.   random 500 < if
  243.     balldy negate to balldy
  244.   then
  245. ;
  246.  
  247. : initvalues ( -- )
  248.   ballsize 2 * pscale * to leftbaty
  249.   screeny ballsize 2 * - batsize - pscale * to rightbaty
  250.   
  251.   screeny pscale * 1000 / to batdy
  252. ;
  253.  
  254. : doreset ( -- )
  255.   resetball
  256.   0 to leftscore
  257.   0 to rightscore
  258.   drawboard
  259. ;
  260.  
  261. : testkey ( map index mask -- bool )
  262.   { map index mask } map index ca+ c@ mask and 0<>
  263. ;
  264.  
  265. : scankeys ( -- )
  266.   " get-key-map" gkbd $call-method
  267.   drop
  268.   dup 0 128 testkey to key_left_up
  269.   dup 0 2 testkey to key_left_down
  270.   dup 4 1 testkey to key_right_up
  271.   dup 5 8 testkey to key_right_down
  272.   dup 6 4 testkey to key_esc
  273.   dup 6 16 testkey to key_off
  274.   drop
  275. ;
  276.  
  277. : moveball ( oldx oldy newx newy -- )
  278.   { oldx oldy newx newy }
  279.   oldx oldy eraseball
  280.   newx newy plotball
  281. ;
  282.  
  283. : doupdateball ( delta -- )
  284.   ballx swap bally swap
  285.   
  286.   dup
  287.   
  288.   balldx * ballx + to ballx
  289.   balldy * bally + to bally
  290.  
  291.   ballx 0< if
  292.     resetball
  293.     balldx abs negate to balldx
  294.     ballx ballsize 2 * pscale * + to ballx
  295.     rightscore 1 + to rightscore
  296.     rightscore 15 = if
  297.       -1 to ballstop
  298.     then
  299.   then
  300.   ballx ball_limit_x > if
  301.     resetball
  302.     balldx abs to balldx
  303.     ballx ballsize 2 * pscale * - to ballx
  304.     leftscore 1 + to leftscore
  305.     leftscore 15 = if
  306.       -1 to ballstop
  307.     then
  308.   then
  309.  
  310.   bally ball_limit_lo_y < if
  311.     balldy negate to balldy
  312.     ball_limit_lo_y 2 * bally - to bally
  313.   then
  314.   bally ball_limit_hi_y > if
  315.     balldy negate to balldy
  316.     ball_limit_hi_y 2 * bally - to bally
  317.   then
  318.   
  319.   balldx 0< if
  320.     ballx hit_limit_left_lo_x hit_limit_left_hi_x between if
  321.       bally leftbaty ballsize pscale * - leftbaty batsize pscale * + between if
  322.         
  323.         bally leftbaty < if
  324.           balldy abs negate to balldy
  325.         then
  326.         
  327.         bally leftbaty batsize ballsize - pscale * + > if
  328.           balldy abs to balldy
  329.         then      
  330.  
  331.         ballx reflect_left_x > if
  332.           balldx abs random 50 / + to balldx
  333.  
  334.           leftbaty bally - unscale
  335.           dup 0 batsize between if
  336.             batsize 2 / - random * 2 / batsize / 25 / balldy + to balldy
  337.           else
  338.             drop
  339.           then
  340.         then
  341.       then
  342.     then
  343.   then
  344.   
  345.   balldx 0> if
  346.     ballx hit_limit_right_lo_x hit_limit_right_hi_x between if
  347.       bally rightbaty ballsize pscale * - rightbaty batsize pscale * + between if
  348.       
  349.         bally rightbaty < if
  350.           balldy abs negate to balldy
  351.         then
  352.         
  353.         bally rightbaty batsize ballsize - pscale * + > if
  354.           balldy abs to balldy
  355.         then      
  356.  
  357.         ballx reflect_right_x < if
  358.           balldx abs random 50 / + negate to balldx
  359.  
  360.           rightbaty bally - unscale
  361.           dup 0 batsize between if
  362.             batsize 2 / - random * 2 / batsize / 25 / balldy + to balldy
  363.           else
  364.             drop
  365.           then
  366.         then
  367.       then
  368.     then
  369.   then
  370.  
  371.   bally ball_limit_lo_y < if
  372.     ball_limit_lo_y to bally
  373.   then
  374.   bally ball_limit_hi_y > if
  375.     ball_limit_hi_y to bally
  376.   then
  377.  
  378.   ballx bally moveball
  379. ;
  380.  
  381. : updateball ( delta -- )
  382.   ballstop 0= if
  383.     doupdateball
  384.   else
  385.     ballstop -1 = if
  386.       drop
  387.     else
  388.       ballstop swap - to ballstop
  389.       ballstop 0<= if
  390.         0 to ballstop
  391.       then
  392.     then
  393.   then
  394. ;
  395.  
  396. : movebatup ( n oldp delta -- )
  397.   { n oldp delta }
  398.   erasecol n calcbatx oldp batsize + delta + ballsize delta negate paintrect
  399.   drawcol n calcbatx oldp delta + ballsize delta negate paintrect
  400. ;
  401.  
  402. : movebatdown ( n oldp delta -- )
  403.   { n oldp delta }
  404.   erasecol n calcbatx oldp ballsize delta paintrect
  405.   drawcol n calcbatx oldp batsize + ballsize delta paintrect
  406. ;
  407.  
  408. : movebat ( n oldy newy -- )
  409.   { n oldy newy }
  410.   newy unscale oldy unscale -
  411.   dup abs batsize < if
  412.     dup 0<> if
  413.       dup 0< if
  414.         n swap oldy unscale swap movebatup
  415.       else
  416.         n swap oldy unscale swap movebatdown
  417.       then
  418.     else
  419.       drop
  420.     then
  421.   else
  422.     drop
  423.     n oldy erasebat
  424.     n newy plotbat
  425.   then
  426. ;
  427.  
  428. : updatebats ( delta -- )
  429.   { delta }
  430.   0 leftbaty 0
  431.   over bat_limit_lo_y > if
  432.   key_left_up 0<> if
  433.     batdy -
  434.   then then
  435.   over bat_limit_hi_y < if
  436.   key_left_down 0<> if
  437.     batdy +
  438.   then then
  439.   delta * over +
  440.   dup to leftbaty
  441.   movebat
  442.   
  443.   1 rightbaty 0
  444.   over bat_limit_lo_y > if
  445.   key_right_up 0<> if
  446.     batdy -
  447.   then then
  448.   over bat_limit_hi_y < if
  449.   key_right_down 0<> if
  450.     batdy +
  451.   then then
  452.   delta * over +
  453.   dup to rightbaty
  454.   movebat
  455. ;
  456.  
  457. : initeverything ( -- )
  458.   cr
  459.   0 to loopcount
  460.   0 to totalupdate
  461.   initvalues
  462.   10 0 do scankeys loop
  463.   doreset
  464.   get-msecs to glastupdate 
  465. ;
  466.  
  467. : doloop ( delta -- )
  468.   loopcount 1 + to loopcount
  469.   dup totalupdate + to totalupdate
  470.   
  471.   dup updatebats
  472.   dup updateball
  473.   redraw
  474.   
  475.   glastupdate + to glastupdate
  476. ;
  477.  
  478. : runpong ( -- )
  479.   initeverything
  480.   begin
  481.     get-msecs glastupdate -
  482.       dup 0> if
  483.         dup 250 > if
  484.           drop
  485.           get-msecs to glastupdate
  486.           250
  487.         then
  488.         doloop
  489.       else
  490.         drop
  491.       then
  492.     scankeys
  493.     key_esc 0<> if
  494.       doreset
  495.     then
  496.   key_off 0<> until
  497.   " Count:" type loopcount s. cr
  498.   " Avg millisec:" type totalupdate loopcount / s. cr
  499. ;
  500.  
  501. 
  502.